package logic

import (
	"context"
	"goods_srv/goods"

	"phone/internal/svc"
	"phone/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type FindGoodsIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFindGoodsIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindGoodsIdLogic {
	return &FindGoodsIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FindGoodsIdLogic) FindGoodsId(req *types.FindGoodsIdRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	findGoods, err := l.svcCtx.GoodsSrv.FindGoods(l.ctx, &goods.FindGoodsIdRequest{Id: int64(req.GoodId)})
	if err != nil {
		return &types.Response{
			Code:    -1,
			Message: "商品不存在",
			Data:    nil,
		}, nil
	}
	if findGoods.Num < int64(req.GoodNum) {
		return &types.Response{
			Code:    -2,
			Message: "商品库存不足",
			Data:    nil,
		}, nil
	}

	return
}
